using UnityEngine;
using System.Collections;

using AssemblyCSharp;

public class FlagCheckpointBehavior : MonoBehaviour 
{
	/// <summary>
	/// The finite state machine runner
	/// </summary>
	private FSMRunner m_Runner;
	
	/// <summary>
	/// The checkpoint manager
	/// </summary>
	private CheckpointManager m_Manager;
	
	// Use this for initialization
	void Start () 
	{
		//create a fsm runner
		m_Runner = new FSMRunner();
		
		//create the states
		IFSMState StateDisabling = new FlagCheckpointStateDisabling(m_Runner, this.gameObject);
		IFSMState StateDisabled = new FlagCheckpointStateDisabled(m_Runner, this.gameObject);
		IFSMState StateEnabling = new FlagCheckpointStateEnabling(m_Runner, this.gameObject);
		IFSMState StateEnabled = new FlagCheckpointStateEnabled(m_Runner, this.gameObject);
		
		//add states
		m_Runner.AddState((int)FlagCheckpointState.eDisabled, StateDisabled);
		m_Runner.AddState((int)FlagCheckpointState.eDisabling, StateDisabling);
		m_Runner.AddState((int)FlagCheckpointState.eEnabled, StateEnabled);
		m_Runner.AddState((int)FlagCheckpointState.eEnabling, StateEnabling);
		
		//get the checkpoint manager
		GameObject goCheckpointManager = GameObject.Find("CheckpointManager");
		m_Manager = goCheckpointManager.GetComponent<CheckpointManager>();
	
		if(m_Manager.SelectedCheckpoint == this)
			Enable();
		else
			Disable();
	}
	
	// Update is called once per frame
	void Update () 
	{
		m_Runner.Update();
	}
	
	void OnTriggerEnter(Collider other)
	{
		
		Debug.Log("trigger collision between Flag Checkpoint and " + other.gameObject.name);
		if(other.gameObject.name != "Player")
			return;
		
		m_Manager.setSelectedCheckpoint(this);
	}
	
	public void Enable()
	{
		m_Runner.SetCurrentState((int)FlagCheckpointState.eEnabling);
	}
	
	public void Disable()
	{
		m_Runner.SetCurrentState((int)FlagCheckpointState.eDisabling);
	}
}
